using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace UFrame
{
    /// <summary>
    /// 活动时间分析[][1-12][1-31][][]
    /// 3-4互不兼容, 3代表1-31号 4代表第几周
    /// </summary>
    public class ActivityDateTool
    {
        public struct LimitTime
        {
            public TimeSpan startTime;
            public TimeSpan endTime;
        }

        public class LimitTimeData
        {
            public List<int> weeks;
            public List<LimitTime> limitTimes;
        }

        public class TimeData
        {
            public List<int> weeks;
            public List<TimeSpan> times;
        }

        public static readonly Regex regex = new Regex(@"\[.*?\]", RegexOptions.Singleline);

        public static LimitTimeData GetLimitTime(string m_limit_time)
        {
            var matchs = regex.Matches(m_limit_time);
            var seconds = matchs[4].Value.Replace("[", String.Empty).Replace("]", string.Empty);
            if (!string.IsNullOrEmpty(seconds))
            {
                LimitTimeData data = new LimitTimeData();
                //data.def = def;
                var week = matchs[3].Value.Replace("[", String.Empty).Replace("]", string.Empty);
                if (!string.IsNullOrEmpty(week))
                {
                    data.weeks = new List<int>();
                    var weeks = week.Split(',');
                    if (weeks.Length > 0 && week.Contains(","))
                    {
                        foreach (var w in weeks)
                        {
                            int day;
                            if (int.TryParse(w, out day))
                            {
                                data.weeks.Add(day);
                            }
                        }
                    }
                    else
                    {
                        weeks = week.Split('-');
                        if (weeks.Length == 2)
                        {
                            int start = int.Parse(weeks[0]);
                            int end = int.Parse(weeks[1]);
                            for (int i = start; i <= end; i++)
                            {
                                data.weeks.Add(i);
                            }
                        }
                        else if (weeks.Length == 1)
                        {
                            int d;
                            if (int.TryParse(weeks[0], out d))
                            {
                                data.weeks.Add(d);
                            }
                        }
                    }
                }

                var secondsArr = seconds.Split(',');
                data.limitTimes = new List<LimitTime>();
                foreach (var second in secondsArr)
                {
                    var arr = second.Split('-');
                    if (arr.Length == 2)
                    {
                        LimitTime lt = default(LimitTime);
                        int start;
                        if (int.TryParse(arr[0], out start))
                        {
                            lt.startTime = new TimeSpan(0, 0, start);
                        }

                        int end;
                        if (int.TryParse(arr[1], out end))
                        {
                            lt.endTime = new TimeSpan(0, 0, end);
                        }

                        data.limitTimes.Add(lt);
                    }
                }

                return data;
            }

            return null;
        }

        /// <summary>
        /// 获取赛季时间
        /// </summary>
        /// <param name="m_time"></param>
        /// <returns></returns>
        public static string GetSeasonTime(string m_time)
        {
            var matchs = regex.Matches(m_time);
            var year = matchs[0].Value.Replace("[", String.Empty).Replace("]", string.Empty);
            var month = matchs[1].Value.Replace("[", String.Empty).Replace("]", string.Empty);
            var day = matchs[2].Value.Replace("[", String.Empty).Replace("]", string.Empty);

            int startyear = 0;
            int endyear = 0;

            int startmonth = 0;
            int endmonth = 0;

            int startday = 0;
            int endday = 0;

            if (!string.IsNullOrEmpty(year))
            {
                string[] years = year.Split('-');
                startyear = int.Parse(years[0]);
                if (years.Length == 1)
                {
                    endyear = startyear;
                }
                else if (years.Length == 2)
                {
                    endyear = int.Parse(years[1]);
                }
            }

            if (!string.IsNullOrEmpty(month))
            {
                string[] months = month.Split('-');
                startmonth = int.Parse(months[0]);
                if (months.Length == 1)
                {
                    endmonth = startmonth;
                }
                else if (months.Length == 2)
                {
                    endmonth = int.Parse(months[1]);
                }
            }

            if (!string.IsNullOrEmpty(day))
            {
                string[] days = day.Split('-');
                startday = int.Parse(days[0]);
                if (days.Length == 1)
                {
                    endday = startday;
                }
                else if (days.Length == 2)
                {
                    endday = int.Parse(days[1]);
                }
            }

            string startyearstr = startyear > 0 ? startyear + "年" : "";
            string startmonthstr = startmonth > 0 ? startmonth + "月" : "";
            string startdaystr = startday > 0 ? startday + "日" : "";

            string endyearstr = endyear > 0 ? endyear + "年" : "";
            string endmonthstr = endmonth > 0 ? endmonth + "月" : "";
            string enddaystr = endday > 0 ? endday + "日" : "";

            return startyearstr + startmonthstr + startdaystr + "-" + endyearstr + endmonthstr + enddaystr;
        }



        public static TimeData GetTime(string m_time)
        {
            var matchs = regex.Matches(m_time);
            var seconds = matchs[4].Value.Replace("[", String.Empty).Replace("]", string.Empty);
            if (!string.IsNullOrEmpty(seconds))
            {
                TimeData data = new TimeData();
                //data.def = def;
                var week = matchs[3].Value.Replace("[", String.Empty).Replace("]", string.Empty);
                if (!string.IsNullOrEmpty(week))
                {
                    data.weeks = new List<int>();
                    var weeks = week.Split(',');
                    if (weeks.Length > 0 && week.Contains(","))
                    {
                        foreach (var w in weeks)
                        {
                            int day;
                            if (int.TryParse(w, out day))
                            {
                                if (day == 7)
                                {
                                    day = 0;
                                }

                                data.weeks.Add(day);
                            }
                        }
                    }
                }

                var secondsArr = seconds.Split(',');
                data.times = new List<TimeSpan>();
                foreach (var second in secondsArr)
                {
                    int time;
                    if (int.TryParse(second, out time))
                    {
                        TimeSpan span = new TimeSpan(0, 0, time);
                        data.times.Add(span);
                    }
                }

                return data;
            }

            return null;
        }
    }
}